package com.example.testapplication

import android.util.Log

/**
 * 倒计时工具
 */
class TimeUtils(s : Long = 200) {
    //运行间隔 默认 200毫秒一次
    private val space = s
    private var thread: Thread? = null
    private var isStop = true
    private var time = 0L

    fun start(i: Long) {
        time = i

        if (isStop) {
            isStop = false
            thread = Thread {
                kotlin.run {
                    while (!isStop && time >= 0) {
                        Log.e("TimeUtils", "循环中--->$time")
                        time -= space
                        Thread.sleep(space)
                    }
                    Log.e("TimeUtils", "停止--->${if(isStop) "手动停止" else "自动停止"}")
                    isStop = true
                }
            }
            thread!!.start()
        }
    }

    fun stop() {
        isStop = true
    }

    fun isStop(): Boolean {
        return isStop
    }
}